package com.cizzy.user.controller;

import cn.hutool.system.UserInfo;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.cizzy.user.common.Result;
import com.cizzy.user.entity.dto.UserDTO;
import com.cizzy.user.entity.User;
import com.cizzy.user.entity.dto.UserHomeDTO;
import com.cizzy.user.entity.dto.UserInfoDTO;
import com.cizzy.user.service.IUserCountService;
import com.cizzy.user.service.IUserService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author crissy-月下闲人
 * @since 2023-10-29
 */
@RestController
@CrossOrigin
@RequestMapping("/user")
@SentinelResource("user")
public class UserController {
    @Resource
    private IUserService userService;

    @Resource
    private IUserCountService userCountService;

    @GetMapping("/authorHome/{id}")
    public Result<UserHomeDTO> authorHome(@PathVariable("id") Long id) {
        return userService.getUserHomeById(id);
    }

    @GetMapping("/testFeign")
    public Result<String> testFeign() {
        System.out.println("----------------------feign调用成功");
        return Result.success("feign调用成功");
    }

    @PostMapping("/init")
    public Result<String> initUser(@RequestHeader("Authorization") String token) {
        return userService.initUser(token);
    }

    @GetMapping("/id/{id}")
    public Result<User> getUserById(@PathVariable("id") Long id) {
        return userService.getUserById(id);
    }

    @PostMapping("/getUserListByIds")
    public Result<List<UserDTO>> getUserListByIds(@RequestBody List<Long> userIdList) {
        return userService.getUserListByIds(userIdList);
    }

    @PostMapping("/login")
    public Result<UserInfoDTO> userLogin(String username, String password) {
        return userService.userLogin(username, password);
    }

    @GetMapping("/logout")
    public Result<String> userLogout(@RequestHeader("Authorization") String token) {
        return userService.userLogout(token);
    }

    @PostMapping("/register")
    public Result<UserInfoDTO> userRegister(User user) {
        return userService.userRegister(user);
    }

    @GetMapping("/deleteUser")
    public Result<String> deleteUser(@RequestHeader("Authorization") String token) {
        return userService.deleteUser(token);
    }


    @GetMapping("/getUserInfoById/{id}")
    public Result<UserInfoDTO> getUserInfoById(@PathVariable("id") Long userId) {
        return userService.getUserInfoById(userId);
    }

    @GetMapping("/getRandomUserIdList/{num}")
    public Result<List<Long>> getRandomUserIdList(@PathVariable("num") Integer num) {
        return userService.getRandomUserIdList(num);
    }
}
